from django.contrib.auth.backends import ModelBackend

from users.models import User


def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'token': token,
        'user_id': user.id,
        'username': user.username
    }


# 定义一个用户认证方法,提供手机号密码登录功能,
class UsernameMobileAuthBackend():
    def authenticate(request, username=None, password=None, **kwargs):
        user = User.objects.get(mobile=username)
        if user is not None and user.check_password(password):
            return user
        else:
            return None
